package com.owen.controller;

import cn.hutool.core.net.URLEncoder;
import com.owen.util.ZxingUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Controller
public class QrCodeController {

    /**
     * 下载二维码
     *
     * @param res response
     * @throws IOException io
     */
    @RequestMapping(value = "qrcode",method = RequestMethod.GET)
    public void download(HttpServletResponse res) throws IOException {
        res.setCharacterEncoding("UTF-8");
        res.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.createDefault().encode("fileName.png", StandardCharsets.UTF_8));
        ClassPathResource cpr = new ClassPathResource("/private/qr_logo.png");
        //生成二维码加logo，底部描述文字
        ZxingUtil.genQRCodeWithLogoAndText("https://gitee.com/", "二维码说明文字", cpr.getInputStream(), res.getOutputStream());
    }

}
